<div id="main">
    <div id="contents">

            <fieldset>
                <legend>
                    <label sid="extension.add"></label>
                </legend>
                <div class="frame_tab_layer">
                    <table class="cmenutitle" cellspacing="0" cellpadding="0" border="0" width="100%">
                        <tbody>
                        <tr>
                            <td class="frame_tab_left">
                                <span id="img401" class="errormsg">&nbsp;</span>
                                <label for="id401">
                                    <label sid="extension.phone"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <input id="id401" type="text" name="id401">
                                <span class="x-form-tip" id="id401Tip"></span>
                            </td>
                        </tr>
                        <tr id="ipphone1">
                            <td class="frame_tab_left">
                                <span id="img591" class="errormsg">&nbsp;</span>
                                <label for="id591">
                                    <label sid="trunk.password"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <input id="id591" type="password" name="id591">
                                <span class="x-form-tip" id="id591Tip"></span>
                            </td>
                        </tr>
                        <tr>
                            <td class="frame_tab_left">
                                <label for="id584">
                                    <label sid="extension.call_barring"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <select id="id584" name="id584">
                                    <option value="0" sid="extension.intercon"></option>
                                    <option value="1" sid="extension.local"></option>
                                    <option value="2" sid="extension.domestic"></option>
                                    <option value="3" sid="extension.Int"></option>
                                    <option value="on" sid="extension.None"></option>
                                </select>
                            </td>
                        </tr>
                        <tr id="tr366">
                            <td class="frame_tab_left">
                                <label for="id366">
                                    <label sid="extension.call_forwarding"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <select id="id366" onchange="read()" name="id366">
                                    <option value="0" sid="extension.call_fwd_close"></option>
                                    <option value="1" sid="extension.fwd_call_to_another"></option>
                                    <option value="2" sid="extension.fwd_unanswered_to_another"></option>
                                    <!--<option value="3" sid="extension.fwd_to_voicemail"></option>
                                    <option value="4" sid="extension.fwd_unanswered_to_voicemail"></option> -->
                                </select>
                            </td>
                        </tr>
                        <tr id="tr367" style="display: none">
                            <td class="frame_tab_left">
                                <span id="img367" class="errormsg">&nbsp;</span>
                                <label for="id367">
                                    <label sid="extension.fwd_number"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <input id="id367" type="text" name="id367">
                            </td>
                        </tr>
                        <!--<tr id="td594">
                            <td class="frame_tab_left">
                                <label for="id594">
                                    <label sid="extension.permanent_recording"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <input id="id594" type="checkbox" name="id594">
                            </td>
                        </tr> -->
                        <tr>
                            <td colspan="2"></td>
                        </tr>
                        <tr>
                            <td class="frame_tab_center" colspan="2">
                                <input id="save_button" class="button" type="button" name="save_button" sid="sbutton.add">
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </fieldset>
            <fieldset>
                <legend>
                    <label sid="trunk.currents"></label>
                </legend>
                <div class="frame_tab_layer">
                    <table id="ipextension" class="cmenutitle" cellspacing="0" cellpadding="0" border="0" width="100%"> </table>
                </div>
            </fieldset>

    </div>
</div>
<br>
<div id="helpContainer">
    <div id="help">
        <div>
            <h2>
                <label sid="share.help"></label>
            </h2>
        </div>
        <div id="helpBody">
            <dl>


                <dt class="term">
                    <label sid="IPPhone.sp1_1"></label>
                </dt>
                <dd class="definition">
                    <label sid="IPPhone.sp1_1Tip"></label>
                </dd>
                <dt class="term">
                    <label sid="IPPhone.sp1_4"></label>
                </dt>
                <dd class="definition">
                    <label sid="IPPhone.sp1_4Tip"></label>
                </dd>

                <dt class="term">
                    <label sid="desktopphones.sp1_2"></label>
                </dt>
                <dd class="definition">
                    <label sid="desktopphones.sp1_2_setp1"></label><br>
                    <label sid="desktopphones.sp1_2_setp2"></label><br><br>
                    <label sid="desktopphones.sp1_2_setp3"></label><br>
                    <label sid="desktopphones.sp1_2_setp4"></label><br><br>
                    <label sid="desktopphones.sp1_2_setp5"></label><br>
                    <label sid="desktopphones.sp1_2_setp6"></label><br><br>
                    <label sid="desktopphones.sp1_2_setp7"></label><br>
                    <label sid="desktopphones.sp1_2_setp8"></label><br><br>
                    <label sid="desktopphones.sp1_2_setp9"></label><br>
                    <label sid="desktopphones.sp1_2_setp10"></label>
                </dd>
                <dt class="term">
                    <label sid="desktopphones.sp1_3"></label>
                </dt>
                <dd class="definition">
                    <label sid="desktopphones.sp1_3_setp1"></label><br>
                    <label sid="desktopphones.sp1_3_setp2"></label><br><br>
                    <label sid="desktopphones.sp1_3_setp3"></label><br>
                    <label sid="desktopphones.sp1_3_setp4"></label><br><br>
                    <label sid="desktopphones.sp1_3_setp5"></label><br>
                    <label sid="desktopphones.sp1_3_setp6"></label>
                </dd>

            </dl>

        </div>
    </div>
</div>

<script type="text/javascript" src="js/libTable.js"></script>
<script type="text/javascript">


    load_html();
    $.formValidator.initConfig({debug:false,submitOnce:true,submitButtonID:"save_button",
        onError:function(msg, obj, errorlist) {

        },
        onSuccess:function(){
            virtual_add();
        }
    });
    $("#id401").formValidator({
        onFocus:errmsg.Info13
    }).regexValidator({
        regExp:regexEnum.notempty,
        onError:errmsg.Info3
    }).regexValidator({
                regExp:regexEnum.num1,
                onError:errmsg.Info1
            });
    $("#id591").formValidator({
        onFocus:""
    }).regexValidator({
                regExp:regexEnum.notempty,
                onError:errmsg.Info3
            });

var virtual_data = new Array();
var old_virtual=0;
var total = 0;
var nullnum =0;
var maxtotal = 0;
var alllist = new Array();

function load_html(){
    if(lineshow("4000")){
        var groupid="&id=4020";
        xmlDataRequest("get_data",groupid,function(data){
            var sendtext = ''; var flag=0;
            $(data).find("item").each(function(){
                if($(this).attr("id")=="4020"){
                    var s=0;
                    $(this).find("value").each(function(){
                        if($.trim($(this).text().split("/")[2])!==""){
                            sendtext += "&line_id="+$.trim($(this).text().split("/")[1]);
                            flag++;
                            total =$.trim($(this).text().split("/")[1]);
                        }else{
                            nullnum=$.trim($(this).text().split("/")[1]);
                        }
                        alllist[s]=$.trim($(this).text().split("/")[1]);
                        s++;
                        maxtotal=$.trim($(this).text().split("/")[1]);
                    });
                    if(total==0){
                        total=parseInt(alllist[0])-1;
                    }
                }
            });
            if(sendtext!==""){
                DataRequest("get_json",sendtext,function(data){
                    virtual_paint_list(data,flag);
                    virtual_data = data;
                });
            }
        });
    }
}

function virtual_paint_list(data,flag){
    if(flag!==0&&flag!==1){
        var data_new=new Array();
        var j=0;
        for(var i=0;i<data.length;i++){
            var line_id = data[i].line_id;
            var id401 = "["+line_id+"]"+replacestr(data[i].id401);
            var id433 = data[i].id433;
            var id591 = replacestr(data[i].id591);
            var id366 = data[i].id366;
            var id367 = data[i].id367;
            //var id594 = replacestr(data[i].id594);
            var id584 = data[i].id584;
            var id447 = data[i].id447;
            var id571 = data[i].id571;
            var id573 = data[i].id573;
            var id600 = data[i].id600;
            if(id573!==""||id571!==""){
                var re_stat = "img_reok|"+extension.online;
            }else{
                var re_stat = "img_refail|"+extension.offline;
            }
            if(id584=="0"){
                id584=extension.intercon;
            }else if(id584=="1"){
                id584=extension.local;
            }else if(id584=="2"){
                id584=extension.domestic;
            }else if(id584=="3"){
                id584=extension.Int;
            }
            if(id447=="on") {
                id584=extension.None;
            }
            if(id366=="0"&&id600=="0") {
                id366="0";
            }else if(id366=="1"&&id600=="0") {
                id366="1";
            }if(id366=="2"&&id600=="0") {
                id366="2";
            }else if(id366=="0"&&id600=="1") {
                id366="3";
            }else if(id366=="0"&&id600=="2") {
                id366="4";
            }
            if(id366=="0"){
                id366="["+id367+"]"+extension.call_fwd_close;
            }else if(id366=="1"){
                id366="["+id367+"]"+extension.fwd_call_to_another+"("+id367+")";
            }else if(id366=="2"){
                id366="["+id367+"]"+extension.fwd_unanswered_to_another+"("+id367+")";
            }else if(id366=="3"){
                id366="["+id367+"]"+extension.fwd_to_voicemail;
            }else if(id366=="4"){
                id366="["+id367+"]"+extension.fwd_unanswered_to_voicemail;
            }
           // data_new.push({id401:id401,id591:id591,id584:id584,id366:id366,id594:id594,re_stat:re_stat});
            data_new.push({id401:id401,id591:id591,id584:id584,id366:id366,re_stat:re_stat});
            j = i;
        }
        var fun_arr = [{type:"modify",name:"virtual_change"},{type:"del",name:"virtual_del"},{type:"other",name:"otherconfig"}];
        var tab = new libTable(new Array(trunk.ID,trunk.password,extension.call_barring,extension.call_forwarding,menu.status,msg.op),data_new,1,10);
        print_table("ipextension",tab,fun_arr, true);
    }else{
        var data_new=new Array();
        var line_id = data.line_id;
        var id401 = "["+line_id+"]"+replacestr(data.id401);
        var id433 = data.id433;
        var id591 = replacestr(data.id591);
        var id366 = data.id366;
        var id367 = data.id367;
        //var id594 = replacestr(data.id594);
        var id584 = data.id584;
        var id447 = data.id447;
        var id571 = data.id571;
        var id573 = data.id573;
        var id600 = data.id600;
        if(id573!==""||id571!==""){
            var re_stat = "img_reok|"+extension.online;
        }else{
            var re_stat = "img_refail|"+extension.offline;
        }

        if(id584=="0"){
            id584=extension.intercon;
        }else if(id584=="1"){
            id584=extension.local;
        }else if(id584=="2"){
            id584=extension.domestic;
        }else if(id584=="3"){
            id584=extension.Int;
        }
        if(id447=="on") {
            id584=extension.None;
        }
        if(id366=="0"&&id600=="0") {
            id366="0";
        }else if(id366=="1"&&id600=="0") {
            id366="1";
        }if(id366=="2"&&id600=="0") {
            id366="2";
        }else if(id366=="0"&&id600=="1") {
            id366="3";
        }else if(id366=="0"&&id600=="2") {
            id366="4";
        }
        if(id366=="0"){
            id366= "["+id367+"]"+extension.call_fwd_close;
        }else if(id366=="1"){
            id366="["+id367+"]"+extension.fwd_call_to_another+"("+id367+")";
        }else if(id366=="2"){
            id366="["+id367+"]"+extension.fwd_unanswered_to_another+"("+id367+")";
        }else if(id366=="3"){
            id366="["+id367+"]"+extension.fwd_to_voicemail;
        }else if(id366=="4"){
            id366="["+id367+"]"+extension.fwd_unanswered_to_voicemail;
        }
       // data_new.push({id401:id401,id591:id591,id584:id584,id366:id366,id594:id594,re_stat:re_stat});
        data_new.push({id401:id401,id591:id591,id584:id584,id366:id366,re_stat:re_stat});
        var fun_arr = [{type:"modify",name:"virtual_change"},{type:"del",name:"virtual_del"},{type:"other",name:"otherconfig"}];
        var tab = new libTable(new Array(trunk.ID,trunk.password,extension.call_barring,extension.call_forwarding,menu.status,msg.op),data_new,1,10);
        print_table("ipextension",tab,fun_arr, true);
    }
}

//function otherconfig(id,id401,id591,id584,id367,id366,id594,re_stat){
function otherconfig(id,id401,id591,id584,id367,id366,re_stat)

{

    setCookie("line_id",id);
    loadNewHtml("ip_extension_more.htm");
}

//function virtual_change(id,id401,id591,id584,id367,id366,id594,re_stat)
function virtual_change(id,id401,id591,id584,id367,id366,re_stat)
{
    old_virtual = parseInt(id);
    $("#id401").val(id401);
    $("#id591").val(id591);
    if(id584==extension.None){
        id584="on";
    }else if(id584==extension.intercon){
        id584="0";
    }else if(id584==extension.local){
        id584="1";
    }else if(id584==extension.domestic){
        id584="2";
    }else if(id584==extension.Int){
        id584="3";
    }
    listselect("id584",id584);

    if(id366.indexOf(extension.call_fwd_close)>=0){
        id366="0";
    }else if(id366.indexOf(extension.fwd_call_to_another)>=0){
        id366="1";
    }else if(id366.indexOf(extension.fwd_unanswered_to_another)>=0){
        id366="2";
    }else if(id366.indexOf(extension.fwd_to_voicemail)>=0){
        id366="3";
    }else if(id366.indexOf(extension.fwd_unanswered_to_voicemail)>=0){
        id366="4";
    }
    listselect("id366",id366);

    $("#id367").val(id367);
   /* if(id594==sip_trunk.table3_td4_option2){
        $("#id594").attr("checked",false);
    }else{
        $("#id594").attr("checked",true);
    }        */
    Modify_button();
    read();
}

function virtual_add(){
    var id401 =$("#id401").val();
   /* if($("#id594").attr("checked")){
        var id594 = "on";
    }else{
        var id594= "off";
    }  */
    var id591 = $("#id591").val();
    var id584 = $("#id584").val();
    var id366 = $("#id366").val();
    var id367= $("#id367").val();
    if(id401==id591){
        alert(errmsg.err103);
        $("#id591").focus();
        return false;
    }
    var settext='';
    if($("#save_button").val()==sbutton.modify){
        settext+='&line_id='+old_virtual;
    }else if(nullnum!=0){
        settext+='&line_id='+nullnum;
        $("#save_button").val(sbutton.add);
    }else if(total < maxtotal){
        settext+='&line_id='+parseInt(parseInt(total)+parseInt(1));
        $("#save_button").val(sbutton.add);
    }
    settext+="&id401="+id401;
    if(settext.indexOf("&line_id=")>=0){
        if(id366=="0") {
            settext+="&id366=0&id600=0";
        }else if(id366=="1") {
            settext+="&id366=1&id600=0";
        }else if(id366=="2") {
            settext+="&id366=2&id600=0";
        }else if(id366=="3") {
            settext+="&id366=0&id600=1";
        }else if(id366=="4") {
            settext+="&id366=0&id600=2";
        }
        if(id584=="on"){
            settext+="&id447=on";
        }else{
            settext+="&id447=off&id584="+$("#id584").val();
        }
       // settext+="&id401="+id401+"&id591="+id591+"&id594="+id594+"&id367="+id367;
        settext+="&id591="+id591+"&id367="+id367;
        submitform(modules["set_data"],settext,"ip_extension_main_load");
    }else{
        alert(msg.lineLimit);
    }
//  load_html();
}

//function virtual_del(id,id401,id591,id584,id367,id366,id594,re_stat){
function virtual_del(id,id401,id591,id584,id367,id366,re_stat){
    if(confirm(msg.confirm_del)){
// show_message("del",msg.deleting);
        //var settext='&line_id='+id+"&id401=&id591=&id594=off&id584=0&id447=on&id366=0&id367=";
        var settext='&line_id='+id+"&id401=&id591=&id584=0&id447=on&id366=0&id367=";
        submitform(modules["set_data"],settext,"ip_extension_main_load");
        // load_html();
    }
}

function read() {
    if($("[name=id366]").val()=='0'||$("[name=id366]").val()=='3'||$("[name=id366]").val()=='4') {
        $("#tr367").hide();
    }else {
        $("#tr367").show();
    }
}
</script>

